package com.dikang.keer.product.service.impl;

import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.Query;
import com.dikang.keer.product.entity.AttrAttrgroupRelationEntity;
import com.dikang.keer.product.entity.AttrEntity;
import com.dikang.keer.product.service.AttrAttrgroupRelationService;
import com.dikang.keer.product.service.AttrService;
import com.dikang.keer.product.vo.AttrGroupVo;
import com.dikang.keer.product.vo.GroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dikang.keer.product.dao.AttrGroupDao;
import com.dikang.keer.product.entity.AttrGroupEntity;
import com.dikang.keer.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class  AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据属性分组查询出下面的属性
     * select * from pms_attrgroup cateloy_id =? and ((attr_group_id = key) or (attr_group_name like key))
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and((obj) ->
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        if (categoryId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }else{
            queryWrapper.eq("catelog_id",categoryId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }

    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrGroup = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<AttrEntity> attrVos = new ArrayList<AttrEntity>();
        attrGroup.stream().map((item)->{
            AttrEntity attrEntity = attrService.getById(item.getAttrId());
            attrVos.add(attrEntity);
            return attrEntity;
        }).collect(Collectors.toList());
        return attrVos;
    }

    /**
     * 批量删除属性组和属性关系
     * @param attrGroupVos
     */
    @Override
    public void deleteAttrGroup(AttrGroupVo[] attrGroupVos) {
        for (AttrGroupVo attrGroupVo : attrGroupVos) {
            attrAttrgroupRelationService.remove(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrGroupVo.getAttrId()).eq("attr_group_id",attrGroupVo.getAttrGroupId()));
        }

    }
    @Override
    public List<GroupWithAttrsVo> getGroupwithAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroups = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<GroupWithAttrsVo> groupWithAttrVos = attrGroups.stream().map(
                (item) -> {
                    GroupWithAttrsVo groupWithAttrVo = new GroupWithAttrsVo();
                    BeanUtils.copyProperties(item, groupWithAttrVo);
                    List<AttrAttrgroupRelationEntity> attrWithgroups = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", groupWithAttrVo.getAttrGroupId()));
                    List<Long> attrIds = attrWithgroups.stream().map(
                            (attrWithgroup) -> {
                                return attrWithgroup.getAttrId();
                            }
                    ).collect(Collectors.toList());
                    if (!attrIds.isEmpty()) {
                        List<AttrEntity> attrEntities = attrService.listByIds(attrIds);
                        groupWithAttrVo.setAttrs(attrEntities);
                    }
                    return groupWithAttrVo;
                }
        ).collect(Collectors.toList());
        return groupWithAttrVos;
    }




}